package com.bw.module_live.anchor.viewmodel

import androidx.lifecycle.viewModelScope
import com.bw.lib_common.base.BaseViewModel
import com.bw.module_live.anchor.intent.LiveAnchorIntent
import com.bw.module_live.anchor.model.repository.CreateLiveRepository
import com.bw.module_live.anchor.state.CreateLiveState
import com.bw.module_live.anchor.state.SendGoodsAddState
import com.bw.module_live.anchor.state.SendGoodsRemovedState
import com.bw.module_live.anchor.state.SendRedPacketState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author ytx
 * @date 2025-10-27 08:17
 * @description
 */
@HiltViewModel
class CreateLiveViewModel @Inject constructor(val repository: CreateLiveRepository):BaseViewModel<LiveAnchorIntent>() {
    private val createLiveState = MutableStateFlow<CreateLiveState>(CreateLiveState.Init)
    val createLiveState_ = createLiveState.asStateFlow()

    private val sendRedPacketState = MutableStateFlow<SendRedPacketState>(SendRedPacketState.Init)
    val sendRedPacketState_ = sendRedPacketState.asStateFlow()

    private val sendGoodsAddState = MutableStateFlow<SendGoodsAddState>(SendGoodsAddState.Init)
    val sendGoodsAddState_ = sendGoodsAddState.asStateFlow()
    private val sendGoodsRemoveState = MutableStateFlow<SendGoodsRemovedState>(SendGoodsRemovedState.Init)
    val sendGoodsRemoveState_ = sendGoodsRemoveState.asStateFlow()



    /**
     * 根据不同的意图网络请求
     */
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is LiveAnchorIntent.CreateLive -> {
                        createLive(it)
                    }
                    is LiveAnchorIntent.SendRedPacket -> {
                        sendRedPacket(it)
                    }
                    is LiveAnchorIntent.SendGoods -> {
                        sendGoods(it)
                    }
                    is LiveAnchorIntent.SendGoodsRemove -> {
                        sendGoodsRemove(it)
                    }
                }
            }
        }
    }

    /**
     * 下车
     */
    private fun sendGoodsRemove(it: LiveAnchorIntent.SendGoodsRemove) {
        viewModelScope.launch {
            repository.sendGoodsRemove(it.goods_id,it.room_id)
                .catch {
                    sendGoodsRemoveState.value = SendGoodsRemovedState.Error
                }.filter {
                    it != null
                }.collect {
                    if (it.code == 200){
                        if (it.data == true){
                            sendGoodsRemoveState.value = SendGoodsRemovedState.Success
                        }
                    } else{
                        sendGoodsRemoveState.value = SendGoodsRemovedState.Error
                    }
                }

        }
    }

    /**
     * 上车
     */

    private fun sendGoods(it: LiveAnchorIntent.SendGoods) {
        viewModelScope.launch {
            repository.sendGoods(it.goods_id,it.room_id)
                .catch {
                    sendGoodsAddState.value = SendGoodsAddState.Error
                }.filter {
                    it != null
                }.collect {
                    if (it.code == 200){
                        if (it.data == true){
                            sendGoodsAddState.value = SendGoodsAddState.Success
                        }
                    } else{
                        sendGoodsAddState.value = SendGoodsAddState.Error
                    }
                }
        }
    }

    /**
     * 发红包
     */
    private fun sendRedPacket(intent: LiveAnchorIntent.SendRedPacket) {
        viewModelScope.launch {
            repository.sendRedPacket(intent.redPacketRequestEntity)
                .catch {
                    sendRedPacketState.value = SendRedPacketState.Error
                }.filter {
                    it != null
                }.collect {
                    if (it.code == 200){
                        if (it.data == true){
                            sendRedPacketState.value = SendRedPacketState.Success
                        }
                    } else{
                        sendRedPacketState.value = SendRedPacketState.Error
                    }
                }
        }
    }

    /**
     * 创建直播间请求，更新状态
     */
    private fun createLive(intent: LiveAnchorIntent.CreateLive) {
        viewModelScope.launch {
            repository.createLiveRoom(intent.liveRequestEntity)
                .catch {
                    createLiveState.value = CreateLiveState.Error
                }
                .filter {
                    it != null
                }
                .collect{
                  if (it.code == 200){
                      createLiveState.value = CreateLiveState.Success(it.data)
                  }else{
                      createLiveState.value = CreateLiveState.Error
                  }
                }
        }
    }
}